package com.cyy.demo.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class RedisUtil{
    private static Map<String, JedisPool> maps = Collections.synchronizedMap(new HashMap());

    public RedisUtil() {
    }

    private static JedisPool getPool(String ip, int port) {
        String key = ip + ":" + port;
        if (!maps.containsKey(key)) {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(500);
            config.setMaxIdle(5);
            config.setMaxWaitMillis(10000L);
            config.setTestOnBorrow(true);
            config.setTestOnReturn(true);
            maps.put(key, new JedisPool(config, ip, port));
        }

        return (JedisPool)maps.get(key);
    }

    private static JedisPool getPool(String ip, int port,String password) {
        String key = ip + ":" + port;
        if (!maps.containsKey(key)) {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(500);
            config.setMaxIdle(5);
            config.setMaxWaitMillis(10000L);
            config.setTestOnBorrow(true);
            config.setTestOnReturn(true);
            maps.put(key, new JedisPool(config, ip, port,2000,password));
        }

        return (JedisPool)maps.get(key);
    }

    public static Jedis getJedis(String ip, int port) {
        Jedis jedis = null;
        int count = 0;

        do {
            try {
                jedis = (Jedis)getPool(ip, port).getResource();
            } catch (Exception var5) {
                returnJedis(jedis, ip, port);
            }

            ++count;
        } while(jedis == null && count < 10);

        return jedis;
    }

    public static Jedis getJedis(String ip, int port,String password) {
        Jedis jedis = null;
        int count = 0;

        do {
            try {
                jedis = (Jedis)getPool(ip, port,password).getResource();
            } catch (Exception var5) {
                returnJedis(jedis, ip, port);
            }

            ++count;
        } while(jedis == null && count < 10);

        return jedis;
    }

    public static void returnJedis(Jedis jedis, String ip, int port) {
        if (jedis != null) {
            getPool(ip, port).returnResource(jedis);
        }

    }

    public static Long sendMesage(Jedis publisher, String channel, String message) throws Exception {
        try {
            return publisher.publish(channel, message);
        } catch (Exception var4) {
            throw new Exception("发送消息失败!");
        }
    }

    public static void receiveMessage(Jedis subscribe, BasicRedisPubSub jedisPubSub, String... channels) throws Exception{
        try {
            subscribe.subscribe(jedisPubSub, channels);
        } catch (Exception var4) {
            var4.printStackTrace();
            throw new Exception("订阅消息失败!");
        }
    }

    public static void receiveMessage(Jedis subscribe, ByteRedisPubSub jedisPubSub, byte[]... channels) throws Exception {
        try {
            subscribe.subscribe(jedisPubSub, channels);
        } catch (Exception var4) {
            var4.printStackTrace();
            throw new Exception("订阅消息失败!");
        }
    }
}
